package cn.chenxinjie.hrm.interceptor;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author 陈鑫杰
 * @date 2022/3/22 16:15
 */
@Component
public class UserInfoInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        // 使用客户端模式获得一个零时授权码(零时授权码中含有初始化权限)
        String url = "http://localhost:4010/oauth/token?client_id=admin&client_secret=1&grant_type=client_credentials&redirect_uri=http://www.baidu.com";
        String result = HttpUtil.get(url);
        Map<String, Object> map = JSON.parseObject(result, Map.class);
        String access_token = (String) map.get("access_token");
        // 将零时授权码设置到请求头中
        requestTemplate.header("Authorization", "bearer " + access_token);
    }
}
